//
//  ChoosePictureViewController.m
//  Drawboard
//
//  Created by A-01 on 2020/6/28.
//  Copyright © 2020 Mac. All rights reserved.
//

#import "ChoosePictureViewController.h"
#import "LineLayout.h"
#import "PictureCollectionViewCell.h"
#import "OriginalViewController.h"

@interface ChoosePictureViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong)UIButton * simpleButton;
@property (nonatomic, strong)UIButton * mediumButton;
@property (nonatomic, strong)UIButton * difficultButton;
@property (nonatomic, strong)UIButton * leftButton;
@property (nonatomic, strong)UIButton * rightButton;
@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic, strong)NSMutableArray * simpleArr;
@property (nonatomic, strong)NSMutableArray * mediumArr;
@property (nonatomic, strong)NSMutableArray * difficultArr;
@property (nonatomic, copy)NSString * type;

@end


@implementation ChoosePictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.type = @"simple";
    [self setupUI];
}


- (void)setupUI{
    
    self.simpleButton = [self setupTopButtonWithTitle:@"simple"];
    self.simpleButton.selected = YES;
    self.simpleButton.layer.borderWidth = 1;
    [self.view addSubview:self.simpleButton];
    self.mediumButton = [self setupTopButtonWithTitle:@"medium"];
    [self.view addSubview:self.mediumButton];
    self.difficultButton = [self setupTopButtonWithTitle:@"difficult"];
    [self.view addSubview:self.difficultButton];
    
    self.leftButton = [self setupLeftAndRgihtButtonWithImg:@"left"];
    [self.view addSubview:self.leftButton];
    self.rightButton = [self setupLeftAndRgihtButtonWithImg:@"right"];
    [self.view addSubview:self.rightButton];
    
    [self.view addSubview:self.collectionView];
}


- (void)clickTopButton:(UIButton *)button{
    
    if (button == self.simpleButton) {
        
        if (!button.selected) {
            
            self.type = @"simple";
            button.selected = YES;
            button.layer.borderWidth = 1;
            
            self.mediumButton.selected = NO;
            self.mediumButton.layer.borderWidth = 0;
            self.difficultButton.selected = NO;
            self.difficultButton.layer.borderWidth = 0;
        }
    }else if (button == self.mediumButton){
        
        if (!button.selected) {
            
            self.type = @"medium";
            button.selected = YES;
            button.layer.borderWidth = 1;
            
            self.simpleButton.selected = NO;
            self.simpleButton.layer.borderWidth = 0;
            self.difficultButton.selected = NO;
            self.difficultButton.layer.borderWidth = 0;
        }
    }else if (button == self.difficultButton){
        
        if (!button.selected) {
            
            self.type = @"difficult";
            button.selected = YES;
            button.layer.borderWidth = 1;
            
            self.simpleButton.selected = NO;
            self.simpleButton.layer.borderWidth = 0;
            self.mediumButton.selected = NO;
            self.mediumButton.layer.borderWidth = 0;
        }
    }
    
    [self.collectionView reloadData];
}


- (void)leftOrRightButton:(UIButton *)button{
    
    NSArray * arr= self.collectionView.indexPathsForVisibleItems;
    if (button == self.leftButton) {
        
        NSInteger index = 99;
        for (NSIndexPath *indexPath in arr) {
            
            NSInteger count = indexPath.item;
            if (count < index) {
                
                index = count;
            }
        }
        NSIndexPath *indexPath = nil;
        indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }else{
        
        NSInteger index = 0;
        for (NSIndexPath *indexPath in arr) {
            
            NSInteger count = indexPath.item;
            if (count > index) {
                
                index = count;
            }
        }
        NSIndexPath *indexPath = nil;
        indexPath = [NSIndexPath indexPathForRow:index inSection:0];
        [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
    }
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([self.comeFrom isEqualToString:@"1"]) {
        
        NSString * image;
        if ([self.type isEqualToString:@"simple"]) {

            image = self.simpleArr[indexPath.row];
        }else if ([self.type isEqualToString:@"medium"]){
            
            image = self.mediumArr[indexPath.row];
        }else{
            
            image = self.difficultArr[indexPath.row];
        }
        
        OriginalViewController * originalVC = [OriginalViewController new];
        originalVC.img = image;
        
        [self.navigationController pushViewController:originalVC animated:YES];
    }else{
        
        if (self.updatePictureBlock) {
            
            NSString * image;
            if ([self.type isEqualToString:@"simple"]) {

                image = self.simpleArr[indexPath.row];
            }else if ([self.type isEqualToString:@"medium"]){
                
                image = self.mediumArr[indexPath.row];
            }else{
                
                image = self.difficultArr[indexPath.row];
            }
            
            self.updatePictureBlock(image);
            
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    if ([self.type isEqualToString:@"simple"]) {

        return self.simpleArr.count;
    }else if ([self.type isEqualToString:@"medium"]){
        
        return self.mediumArr.count;
    }else{
        
        return self.difficultArr.count;
    }
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    PictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCellId" forIndexPath:indexPath];
    
    if ([self.type isEqualToString:@"simple"]) {
        
        cell.imageView.image = [UIImage imageNamed:self.simpleArr[indexPath.row]];
    }else if ([self.type isEqualToString:@"medium"]){
        
        cell.imageView.image = [UIImage imageNamed:self.mediumArr[indexPath.row]];
    }else if ([self.type isEqualToString:@"difficult"]){
        
        cell.imageView.image = [UIImage imageNamed:self.difficultArr[indexPath.row]];
    }
    return cell;
}


-(void)viewDidLayoutSubviews{
    
    [super viewDidLayoutSubviews];
    
    [self.simpleButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.backButton);
        make.leading.mas_equalTo(self.backButton.mas_trailing).offset(35);
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(45);
    }];

    [self.mediumButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.simpleButton);
        make.leading.mas_equalTo(self.simpleButton.mas_trailing).offset(35);
    }];

    [self.difficultButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.width.height.mas_equalTo(self.mediumButton);
        make.leading.mas_equalTo(self.mediumButton.mas_trailing).offset(35);
    }];
    
    [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(self.view).offset(35);
        make.centerY.mas_equalTo(self.view).offset(10);
        make.width.height.mas_equalTo(44);
    }];
    
    [self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.mas_equalTo(self.view).offset(-25);
        make.centerY.width.height.mas_equalTo(self.leftButton);
    }];
}

- (UIButton *)setupTopButtonWithTitle:(NSString *)title{
    
    UIButton * button = [UIButton new];
    
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:ZRBlackColor forState:UIControlStateNormal];
    [button setBackgroundImage:[self createImageWithColor:ZRWhiteColor] forState:UIControlStateNormal];
    [button setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"EDFF1C"]] forState:UIControlStateSelected];
    [button addTarget:self action:@selector(clickTopButton:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.font = [UIFont boldSystemFontOfSize:18];
    button.layer.cornerRadius = 16;
    button.layer.borderColor = [ZRBlackColor CGColor];
    button.layer.masksToBounds = YES;
    
    return button;
}


- (UIButton *)setupLeftAndRgihtButtonWithImg:(NSString *)img{
    
    UIButton * button = [UIButton new];
    
    [button setImage:[UIImage imageNamed:img] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(leftOrRightButton:) forControlEvents:UIControlEventTouchUpInside];

    return button;
}


- (UICollectionView *)collectionView{
    if (_collectionView == nil) {
        LineLayout *layout = [[LineLayout alloc] init];
        layout.sectionInset = UIEdgeInsetsMake(50, kScreenW/2 - 177/2, 50, kScreenW/2 - 177/2);
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(100, 100, kScreenW - 200, 300) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
//        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"PictureCellId"];
    }
    return _collectionView;
}


- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}


- (NSMutableArray *)simpleArr{
    
    if (!_simpleArr) {
        
        _simpleArr = [NSMutableArray arrayWithCapacity:15];
        for (int i = 0; i < 15; i++) {
            
            NSString * ss = [NSString stringWithFormat:@"s_%d",i];
            [_simpleArr addObject:ss];
        }
    }
    
    
    return _simpleArr;;
}

- (NSMutableArray *)mediumArr{
    
    if (!_mediumArr) {
        
        _mediumArr = [NSMutableArray arrayWithCapacity:9];
        for (int i = 0; i < 9; i++) {
            
            NSString * ss = [NSString stringWithFormat:@"m_%d",i];
            [_mediumArr addObject:ss];
        }
    }
    
    return _mediumArr;
}


- (NSMutableArray *)difficultArr{
    
    if (!_difficultArr) {
        
        _difficultArr = [NSMutableArray arrayWithCapacity:5];
        for (int i = 0; i < 5; i++) {
            
            NSString * ss = [NSString stringWithFormat:@"d_%d",i];
            [_difficultArr addObject:ss];
        }
    }
    
    return _difficultArr;
}

@end
